﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using ShkSimulation.carsharing;
using ShkSimulation.core.dao.input.loader;
using ShkSimulation.core.dao.input.resource;
using ShkSimulation.desktop.component.tools;
using ShkSimulation.desktop.setting;
using ShkSimulation.desktop.util;

namespace ShkSimulation.desktop {
    
    partial class ShkSimulationForm{
        
        private void Init() {
            自动初始化ToolStripMenuItem.Checked = ConfigManager.UserSystemConfig.自动初始化;
            BindPerformance();
            if (ConfigManager.UserSystemConfig.自动初始化) {
                this.Shown += CreateAndInitSimulation;
            }

            void BindPerformance() {
                PerformanceCheckBox.DataBindings.Clear();
                this.PerformanceCheckBox.CheckedChanged -= PerformanceCheckBoxOnCheckedChanged;
                this.RealTimePerformanceStatisticsTool = new RealTimePerformanceStatisticsTool(ConfigManager.UserSystemConfig.性能统计);
                PerformanceCheckBox.DataBindings.Add("Checked", ConfigManager.UserSystemConfig, "性能统计", false, DataSourceUpdateMode.OnPropertyChanged);
                this.PerformanceCheckBox.CheckedChanged += PerformanceCheckBoxOnCheckedChanged;
                
                void PerformanceCheckBoxOnCheckedChanged(object? sender, EventArgs e) {
                    RealTimePerformanceStatisticsTool.SetEnabled(PerformanceCheckBox.Checked);
                }

            }
        }

        private void 自动初始化ToolStripMenuItem_CheckedChanged(object sender, EventArgs e) {
            ConfigManager.UserSystemConfig.自动初始化 = 自动初始化ToolStripMenuItem.Checked;
        }
        
        private void 重置ToolStripMenuItem_Click(object sender, EventArgs e) {
            CheckAndResetSimulation(sender, e);
        }
        
        private void 重置初始化ToolStripMenuItem_Click(object sender, EventArgs e) {
            CheckResetAndInitSimulation(sender, e);
        }
        
        private void 重置设置ToolStripMenuItem_Click(object sender, EventArgs e) {
            MessageBoxDialogResult messageResult = MessageBoxForm.GetMessageResult(@"提醒", @"要重置所有设置吗？", false, MessageBoxImage.Warning);
            if (messageResult == MessageBoxDialogResult.OK) {
                ConfigManager.ResetSystemConfig();
                ConfigManager.ResetRunConfig();
            }
            Init();
        }

        private void 站台文件ToolStripMenuItem_Click(object sender, EventArgs e) {
            string filePath = FileUtil.OpenFile(InputOpenFileDialog, "选择站台文件");
            if (filePath == null) {
                return;
            }
            ConfigManager.UserSimulationConfig.站台文件 = filePath;
        }

        private void 道路文件ToolStripMenuItem_Click(object sender, EventArgs e) {
            string filePath = FileUtil.OpenFile(InputOpenFileDialog, "选择道路文件");
            if (filePath == null) {
                return;
            }
            ConfigManager.UserSimulationConfig.道路文件 = filePath;
        }

        private void OD文件ToolStripMenuItem_Click(object sender, EventArgs e) {
            string filePath = FileUtil.OpenFile(InputOpenFileDialog, "选择需求文件");
            if (filePath == null) {
                return;
            }
            ConfigManager.UserSimulationConfig.需求文件 = filePath;
        }

        private void 导航ToolStripMenuItem_Click(object sender, EventArgs e) {
            string filePath = FileUtil.OpenFile(InputOpenFileDialog, "选择导航文件");
            if (filePath == null) {
                return;
            }
            ConfigManager.UserSimulationConfig.导航文件 = filePath;
        }
        
        private void 保存所有配置ToolStripMenuItem_Click(object sender, EventArgs e) {
            string saveFilePath = FileUtil.SaveFile(SaveFileDialog, @"配置文件", @"配置文件|*.pkconfig", "shk.pkconfig");
            if (saveFilePath == null) {
                return;
            }
            ConfigManager.BackupConfigsByJson(saveFilePath);
            Console.WriteLine($@"导出配置文件成功：{saveFilePath}");
        }

        private void 车站ToolStripMenuItem_Click(object sender, EventArgs e) {
            ExportSimulationData(application?.SimulationNetwork?.NetworkInput?.AllResources.GetValueOrDefault(CarsharingConfig.StationResourceKey), "station", "车站");
        }

        private void 道路ToolStripMenuItem_Click(object sender, EventArgs e) {
            ExportSimulationData(application?.SimulationNetwork?.NetworkInput?.AllResources.GetValueOrDefault(CarsharingConfig.RoadResourceKey), "road", "道路");
        }

        private void 需求ToolStripMenuItem_Click(object sender, EventArgs e) {
            ExportSimulationData(application?.SimulationNetwork?.NetworkInput?.AllResources.GetValueOrDefault(CarsharingConfig.DemandResourceKey), "demand", "需求");
        }
        
        private void 导航ToolStripMenuItem1_Click(object sender, EventArgs e) {
            if (application?.SimulationNetwork?.NetworkInput == null) {
                MessageBox.Show(@"当前没有已加载仿真", @"提示");
                return;
            }
            string path = application.SimulationNetwork.NetworkInput.ConfigInstance.GetConfigValue<string>(CarsharingConfig.NavigateRouterPathKey);
            if (path == null || !File.Exists(path)) {
                MessageBox.Show(@"没有启用导航模式或没有找到导航文件", @"提示");
                return;
            }
            ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe");
            psi.Arguments = "/e,/select," + path;
            Process.Start(psi);
        }
        
        private void ExportSimulationData(Resource resource, string filePrefix, string description) {
            if (resource == null) {
                MessageBox.Show(@"当前没有已加载仿真", @"提示");
                return;
            }
            string saveFilePath = FileUtil.SaveFile(SaveFileDialog, $@"保存{description}文件", $@"{description}数据|*.xlsx", $"{filePrefix}.xlsx");
            if (saveFilePath == null) {
                return;
            }
            ExcelResourceLoader loader = new ExcelResourceLoader();
            if (loader.SaveData(resource, saveFilePath)) {
                Console.WriteLine($@"导出{description}数据成功：{saveFilePath}");
            } else {
                Console.WriteLine($@"导出{description}数据失败：{saveFilePath}");
            }
        }

    }
}